package cn.com.dcsgo.service.impl;

import cn.com.dcsgo.mapper.LeaderMapper;
import cn.com.dcsgo.mapper.UserDeliveryMapper;
import cn.com.dcsgo.mapper.UserMapper;
import cn.com.dcsgo.model.user.Leader;
import cn.com.dcsgo.model.user.User;
import cn.com.dcsgo.model.user.UserDelivery;
import cn.com.dcsgo.service.UserService;
import cn.com.dcsgo.vo.user.LeaderAddressVo;
import cn.com.dcsgo.vo.user.UserLoginVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * UserServiceImpl
 *
 * @author Dcsgo
 * @since 2025/4/24 10:59
 */
@Service
@RequiredArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    private final UserMapper userMapper;
    private final UserDeliveryMapper userDeliveryMapper;
    private final LeaderMapper leaderMapper;

    @Override
    public User getByOpenId(String openId) {
        return userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getOpenId, openId));
    }

    @Override
    public LeaderAddressVo getLeaderAndDeliveryInfo(Long userId) {
        //获取默认自提点信息，它还与团长关联
        UserDelivery userDelivery = getDefaultDeliveryByUserId(userId);
        if (userDelivery == null) {
            return null;
        }
        //获取默认自提点关联的团长信息
        Leader leader = leaderMapper.selectById(userDelivery.getLeaderId());

        //封装默认自提点和团长信息
        LeaderAddressVo leaderAddressVo = new LeaderAddressVo();
        BeanUtils.copyProperties(leader, leaderAddressVo);
        leaderAddressVo.setUserId(userId);
        leaderAddressVo.setLeaderId(leader.getId());
        leaderAddressVo.setLeaderName(leader.getName());
        leaderAddressVo.setLeaderPhone(leader.getPhone());
        leaderAddressVo.setWareId(userDelivery.getWareId());
        leaderAddressVo.setStorePath(leader.getStorePath());
        return leaderAddressVo;
    }

    @Override
    public UserLoginVo getUserLoginVo(Long userId) {
        //获取用户基本信息
        User user = userMapper.selectById(userId);
        UserLoginVo userLoginVo = new UserLoginVo();
        userLoginVo.setNickName(user.getNickName());
        userLoginVo.setUserId(userId);
        userLoginVo.setPhotoUrl(user.getPhotoUrl());
        userLoginVo.setOpenId(user.getOpenId());
        userLoginVo.setIsNew(user.getIsNew());

        //获取默认自提点和团长信息
        UserDelivery userDelivery = getDefaultDeliveryByUserId(userId);
        if (userDelivery != null) {
            userLoginVo.setLeaderId(userDelivery.getLeaderId());
            userLoginVo.setWareId(userDelivery.getWareId());
        } else {
            userLoginVo.setLeaderId(-1L);
            userLoginVo.setWareId(-1L);
        }
        return userLoginVo;
    }

    @Override
    public UserDelivery getDefaultDeliveryByUserId(Long userId) {
        return userDeliveryMapper.selectOne(
                new LambdaQueryWrapper<UserDelivery>()
                        .eq(UserDelivery::getUserId, userId)
                        .eq(UserDelivery::getIsDefault, 1)//1为默认自提点
        );
    }
}
